package org.point.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.point.common.controller.BaseController;
import org.point.common.util.DoPage;
import org.point.domain.CreditsType;
import org.point.domain.Product;
import org.point.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @ClassName: ProductController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Kenny_Z' szqiuk@gmail.com
 * @date 2016年10月23日 下午9:18:34
 *
 */
@Controller
@RequestMapping("/product/")
public class ProductController extends BaseController {

	@Autowired
	private ProductService service;

	/**
	 * 
	 * @Title: creditsTypeList @Description: TODO(查询积分规则分类) @param @param
	 *         memberId @param @param currentPage @param @param
	 *         userLevelId @param @param keyWord @param @param response
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "productList.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void productList(@RequestParam(value = "memberId") String memberId,
			@RequestParam(value = "currentPage", required = false) Integer currentPage,
			@RequestParam(value = "keyWord", required = false) String keyWord,
			@RequestParam(value = "upstarttime", required = false) String upstarttime,
			@RequestParam(value = "upendtime", required = false) String upendtime,
			@RequestParam(value = "downstarttime", required = false) String downstarttime,
			@RequestParam(value = "downendtime", required = false) String downendtime, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		DoPage<Product> page = new DoPage<Product>();
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			if (memberId != null) {

				map.put("memberId", memberId);
				page.setPageSize(10);
				if (currentPage != null) {
					page.setCurrentPage(currentPage);
				}
				map.put("firstRecord", page.getFirstRecord());
				map.put("pageSize", page.getPageSize());

				if (strOperateUtil.isNullForString(keyWord)) {
					map.put("keyWord", "%" + keyWord + "%");
				}
				if (strOperateUtil.isNullForString(upstarttime) && strOperateUtil.isNullForString(upendtime)) {
					map.put("upstarttime", upstarttime);
					map.put("upendtime", upendtime);
				}
				if (strOperateUtil.isNullForString(downstarttime) && strOperateUtil.isNullForString(downendtime)) {
					map.put("downstarttime", downstarttime);
					map.put("downendtime", downendtime);
				}

				page.setMap(map);
				// 查询积分规则分页数据
				DoPage<Product> resultPage = service.searchProForPage(page);
				// 返回分页参数，及积分规则分类数据
				jsonObj.put("page", resultPage);
				// 查询积分规则成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 会员单位ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", 0);
			throw new RuntimeException("查询积分规则分类错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (IOException e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: getCreditsType @Description: TODO(根据商品ID，查询详细信息) @param @param
	 *         creditsTypeId 设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "getPRO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void getProduct(@RequestParam(value = "id") String id, HttpServletResponse response) {

		JSONObject jsonObj = new JSONObject();
		Map<String, Object> map = new HashMap<String, Object>();
		Integer result = 0;

		try {
			if (strOperateUtil.isNullForString(id)) {
				map.put("id", Integer.parseInt(id));
				Product record = service.getPROByPROID(map);
				System.out.println(record.getProductName());
				jsonObj.put("record", record);
				// 查询积分规则详细信息，成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类ID为null
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("查询积分规则分类详细信息！错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: insertCT @Description: TODO(新增积分规则分类) @param @param record
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "insertPRO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void insertCT(@ModelAttribute("Product") Product record, HttpServletResponse response) {

		// 测试
		String memberId = "member001";

		JSONObject jsonObj = new JSONObject();
		Integer result = 0;

		try {
			if (record != null) {
				record.setCreateDt(new Date());
				record.setCreateOper("Admin");
				record.setDelFlag(new Byte("0"));
				record.setMemberId(memberId);
				String pt_code = getNextNo("PT");
				result = service.insertPRO(record, pt_code);
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", result);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

	/**
	 * 
	 * @Title: delCT @Description: TODO(批量删除积分规则分类) @param @param record
	 *         设定文件 @return void 返回类型 @throws
	 */
	@RequestMapping(value = "delPRO.do", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public void delPRO(@RequestParam(value = "ids") String ids, HttpServletResponse response) {
		// 测试
		JSONObject jsonObj = new JSONObject();
		Integer result = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		String newIDs = ids.substring(9, ids.length() - 1);
		try {
			map.put("newIDs", newIDs);
			result = service.deletePROs(map);
			// result = service.deleteCT();
			if (result > 0) {
				// 积分规则分类新增成功
				jsonObj.put("resultMsg", 1);
			} else {
				// 积分规则分类参数为null， 请检查参数！
				jsonObj.put("resultMsg", -1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonObj.put("resultMsg", result);
			throw new RuntimeException("积分规则分类新增错误！", e);
		} finally {
			try {
				feedback(response, jsonObj.toString());
			} catch (Exception e) {
				throw new RuntimeException("网络错误，请联系管理员！", e);
			}
		}

	}

}
